package com.example.zce.back.controller;

import com.example.zce.back.common.oss.AliOSS;
import com.example.zce.library.common.bean.ResultMapBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * 描述：
 *
 * @author zengyz
 * @date 2018/8/8 9:52
 */

@Controller
@RequestMapping("/testing")
public class TestingController {

    @Autowired
    private AliOSS aliOSS;

    @GetMapping("/index.html")
    public ModelAndView indexHtml() {
        return new ModelAndView("/test/index");
    }

    @ResponseBody
    @PostMapping("/upload.json")
    public ResultMapBean uploadImg(MultipartHttpServletRequest request) {
        List<MultipartFile> file = request.getMultiFileMap().get("file");
        if (!file.isEmpty()) {
            try {
                byte[] bytes = file.get(0).getBytes();
                String imgAddress = aliOSS.uploadOSS("TEST", bytes);
                return ResultMapBean.ok(imgAddress);
            } catch (IOException e) {
                return ResultMapBean.error(500,"上传失败");
            }
        }else{
            return ResultMapBean.error(500,"上传失败");
        }
    }
}
